<?php
# 文件名称:access.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
defined( '_SYS' ) or die( 'No direct script access allowed' );
class C_htmlAccess
{
	function user($id)
	{
		$db = C_loader::getDBO();
		$db->setQuery(
			'SELECT a.admin_id' .
			' FROM '.$db->_table_prefix.'admin_table AS a'.
			' WHERE a.id='.$id
		);		
		$ret = $db->loadResult();
		return $ret;
	}
	/**
	 * Displays a list of the available user groups.
	 *
	 */
	function usergroup($name, $selected, $attribs = 'class="inputbox"', $disabled = null, $section = '')
	{
		$db = C_loader::getDBO();
		$db->setQuery(
			'SELECT a.id AS value, a.title AS text, COUNT(DISTINCT b.id) AS level' .
			' FROM '.$db->_table_prefix.'usergroups AS a' .
			' LEFT JOIN `'.$db->_table_prefix.'usergroups` AS b ON a.lft > b.lft AND a.rgt < b.rgt' .
			' '.$section.
			' GROUP BY a.id' .
			' ORDER BY a.lft ASC'
		);
		$options = $db->loadObjectList();
		
		// Check for a database error.
		if ($db->getErrorNum()) {
			return null;
		}
		for ($i=0,$n=count($options); $i < $n; $i++)
		{
			$options[$i]->text = str_repeat('- ',$options[$i]->level).$options[$i]->text;
			if($disabled != null && $options[$i]->value==$disabled)
			{
				unset($options[$i]);
			}
		}
		$tmp = new stdClass();
		$tmp->value = 0;
		$tmp->text = '- '.C_text::_('SelectType').' '.C_text::_('Usergroups').' -';
		array_unshift($options,$tmp);
		
		return C_html::_('select.genericlist', $options, $name, $attribs, 'value', 'text', $selected);
	}
	/**
	 * Returns a UL list of user groups with check boxes
	 *
	 */
	function usergroups($name, $selected)
	{
		$num = 0;
		$num++;
		$db = C_loader::getDBO();
		$db->setQuery(
			'SELECT a.*, COUNT(DISTINCT b.id) AS level' .
			' FROM '.$db->_table_prefix.'usergroups AS a' .
			' LEFT JOIN `'.$db->_table_prefix.'usergroups` AS b ON a.lft > b.lft AND a.rgt < b.rgt' .
			' GROUP BY a.id' .
			' ORDER BY a.lft ASC'
		);
		$groups = $db->loadObjectList();
		
		// Check for a database error.
		if ($db->getErrorNum()) {
			return null;
		}
		$html = array();
		$html[] = '<ul class="checklist usergroups">';
		for ($i=0, $n=count($groups); $i < $n; $i++)
		{
			$item = &$groups[$i];
			// Setup  the variable attributes.
			$eid = $num.'group_'.$item->id;
			$checked = in_array($item->id, $selected) ? ' checked="checked"' : '';
			$rel = ($item->parent_id > 0) ? ' rel="'.$num.'group_'.$item->parent_id.'"' : '';
			// Build the HTML for the item.
			$html[] = '	<li>';
			$html[] = '		<label for="'.$eid.'">';
			$html[] = '			<input type="checkbox" name="'.$name.'[]" value="'.$item->id.'" id="'.$eid.'"';
			$html[] = '				'.$checked.$rel.' />';
			$html[] = '			'.str_repeat('- ', $item->level).$item->title;
			$html[] = '		</label>';
			$html[] = '	</li>';
		}
		$html[] = '</ul>';
		return implode("\n", $html);
	}
	function user_to_usergroup($gid)
	{		
		$gid = explode(",",$gid);
		$db = C_loader::getDBO();
		$ret = array();
		for($i=0;$i<count($gid);$i++)
		{
			$db->setQuery(
				'SELECT a.title FROM `'.$db->_table_prefix.'usergroups` AS a '.
				'WHERE a.id='.$gid[$i]
			);
			$ret[]= $db->loadResult();
		}		
		return implode(",", $ret);
	}
	/**
	 * Returns a UL list of user groups with check boxes
	 *
	 */
	function actions($name, $selected)
	{
		
		$n = 0;
		$n++;
		_import('user/access/simplerule',false);
		$actions	= C_simpleRule::getRules('user', 1);
		$html		= array();
		$html[]		= '<ul class="checklist access-actions">';
		for ($i=0, $n=count($actions); $i < $n; $i++)
		{
			$item = &$actions[$i];
			// Setup  the variable attributes.
			$eid = $n.'action_'.$item->id;
			$checked = in_array($item->id, $selected) ? ' checked="checked"' : '';
			// Build the HTML for the item.
			$html[] = '	<li>';
			$html[] = '		<label for="'.$eid.'">';
			$html[] = '			<input class="cb" type="checkbox" name="'.$name.'[]" value="'.$item->id.'" id="'.$eid.'"';
			$html[] = '				'.$checked.' />';
			$html[] = '			'.C_text::_($item->title).$item->title;
			$html[] = '		</label>';
			$html[] = '	</li>';
		}
		$html[] = '</ul>';
		return implode("\n", $html);
	}
	function tb_actions($name, $groupid, $section)
	{		
		$n = 0;
		$n++;
		_import('user/access/simplerule',false);		
		$actions	= C_simpleRule::getRules($section, 1);		
		$selected = array();
		// Get all the core type 1 rules
		$rule = C_simpleRule::getInstance();
		foreach ($actions as $action)
		{
			$rule->load($action->name);
			// Get the user groups mapped to this rule
			$groups		= $rule->getUserGroups();
			// Is this group in this rule
			if (in_array($groupid, $groups)) {
				$selected[] = $action->id;
			}
		}
		
		
		$html		= array();
		$html[]		= '<table class="adminlist">';
		$html[] 	= '
		<thead>
			<tr>
				<th class="title">
					#
				</th>
				<th>
					'.C_text::_('ACCESS').'
				</th>
				<th>
					'.C_text::_('ALLOW').'
				</th>
			</tr>
		</thead>
		<tbody>
		';
		$k = 0;
		for ($i=0, $n=count($actions); $i < $n; $i++)
		{
			$item = &$actions[$i];
			// Setup  the variable attributes.
			$eid = $n.'action_'.$item->id;
			$checked = in_array($item->id, $selected) ? ' checked="checked"' : '';
			// Build the HTML for the item.
			$html[] = '	<tr class="row'.$k.'">';
			$html[] = ' 	<td align="center">
									'.($i+1).'
							</td>
					  ';
			$html[] = ' 	<td align="center">
									'.C_text::_($item->title).'
							</td>
					  ';
			$html[] = ' 	<td align="center">
									<input class="cb" type="checkbox" name="'.$name.'[]" value="'.$item->id.'" id="'.$eid.'"  '.$checked.' />
							</td>
					  ';
			$html[] = '	</tr>';
			$k = 1 - $k;
		}
		$html[] = '</tbody></table>';
		return implode("\n", $html);
	}
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>